package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.JsonUtils;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.PointRulesBo;
import com.bigdata.service.domain.vo.PointRulesVo;
import com.bigdata.service.domain.PointRules;
import com.bigdata.service.mapper.PointRulesMapper;
import com.bigdata.service.service.IPointRulesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分规则配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class PointRulesServiceImpl implements IPointRulesService {

    private final PointRulesMapper baseMapper;

    /**
     * 查询积分规则配置
     */
    @Override
    public PointRulesVo queryById(String ruleId){
        return baseMapper.selectVoById(ruleId);
    }

    /**
     * 查询积分规则配置列表
     */
    @Override
    public TableDataInfo<PointRulesVo> queryPageList(PointRulesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PointRules> lqw = buildQueryWrapper(bo);
        Page<PointRulesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        log.info(JsonUtils.toJsonString(result));
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分规则配置列表
     */
    @Override
    public List<PointRulesVo> queryList(PointRulesBo bo) {
        LambdaQueryWrapper<PointRules> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PointRules> buildQueryWrapper(PointRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PointRules> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), PointRules::getRuleName, bo.getRuleName());
        lqw.eq(StringUtils.isNotBlank(bo.getActionType()), PointRules::getActionType, bo.getActionType());
        lqw.eq(bo.getPointValues() != null, PointRules::getPointValues, bo.getPointValues());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), PointRules::getDescription, bo.getDescription());
        lqw.eq(bo.getStatus() != null, PointRules::getStatus, bo.getStatus());
        lqw.eq(bo.getCapValue() != null, PointRules::getCapValue, bo.getCapValue());
        return lqw;
    }

    /**
     * 新增积分规则配置
     */
    @Override
    public Boolean insertByBo(PointRulesBo bo) {
        PointRules add = BeanUtil.toBean(bo, PointRules.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    /**
     * 修改积分规则配置
     */
    @Override
    public Boolean updateByBo(PointRulesBo bo) {
        PointRules update = BeanUtil.toBean(bo, PointRules.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PointRules entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分规则配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
